package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.handUtil.SearchKeywordUtil;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component("I1023")
@Scope("prototype")
public class I1023 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(I1023.class);

    @Autowired
    private HandingData handingData;

    @Autowired
    private SearchKeywordUtil searchKeywordUtil;

    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取I1023组件M");
        List<ComponentResult> componentResults = checkRulesI1023("M-4",getCatalogId(),"M-4",getRuleTitle(),"(M9)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取I1023组件BM");
        List<ComponentResult> componentResults = checkRulesI1023("BM-4","BM-4","BM-4",getRuleTitle(),"(M9)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取I1023组件DM");
        List<ComponentResult> componentResults = checkRulesI1023("DM-4","DM-4","DM-4",getRuleTitle(),"(D9)");
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取I1023组件BDM");
        List<ComponentResult> componentResults = checkRulesI1023("BDM-4","BDM-4","BDM-4",getRuleTitle(),"(D9)");
        return componentResults;
    }

    private List<ComponentResult> checkRulesI1023(String model,String catalogId,String chapterId,String ruleName,String subTile){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int startPage = 0;
        String bczjKeyword = "补充项目资本金";
        int bczjPageNum = 0;
        List<ComponentResult.TextPosition> bczjContentAndPositionList = null;
        boolean bczjFlag = false;

        String yjyaKeyword = "在应急预案中增加出现重大事项或特定原因而导致募集资金无法用于项目资本金的约定处理机制";
        int yjyaPageNum = 0;
        List<ComponentResult.TextPosition> yjyaContentAndPositionList = null;
        boolean yjyaFlag = false;

        String[] keywords = new String[]{"项目投资金额","投资期限","投资计划","投资资金安排","资本金已到位情况","已取得批文情况","承诺项目合法合规","符合国家相关产业政策","不存在不合规行为","专户资金监管"};
        boolean keywordFlag = false;
        int keywordPageNum = 0;
        List<ComponentResult.TextPosition> keywordContentAndPositionList = null;

        int bcxmzbjPageNum = 0;
        List<ComponentResult.TextPosition> bcxmzbjContentAndPositionList = null;
        boolean bcxmzbj = false;
        boolean isAllErr = false;
        boolean isAllSeem = false;
        try {

            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage = chapterModel!=null?chapterModel.getStartPage():0;
                if (chapterModel!=null){

                    List<PageModel> pageModelList = chapterModel.getPageModelList();
                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        for (PageModel pageModel : pageModelList){
                           // Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), bczjKeyword);
                            // Map<String, Object> containsKeyWord = handingData.isContainsKeyWord(pageModel.getJsonTextData(), bczjKeyword);
                            Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), bczjKeyword,pageModel);

                            bczjFlag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                            if (bczjFlag){
                                bczjPageNum = pageModel.getPageNumber();
                                bczjPageNum = bczjPageNum!=0?bczjPageNum:startPage;
                                bczjContentAndPositionList = (List<ComponentResult.TextPosition>)containsKeyWordOfRegex2Lines.get("position");
                                break;
                            }
                        }

                        if (bczjFlag){
                            componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTile+"关键词检查-"+bczjKeyword,"正确",false,getRegulationId(),bczjPageNum,bczjContentAndPositionList,"01");
                            componentResults.add(componentResult);

                            for (String keyword: keywords){
                                for (PageModel pageModel : pageModelList){
                                  //  Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), keyword);
                                    // Map<String, Object> containsKeyWord = handingData.isContainsKeyWord(pageModel.getJsonTextData(), keyword);
                                    Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), keyword,pageModel);

                                    keywordFlag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                                    if (keywordFlag){
                                        keywordPageNum = pageModel.getPageNumber();
                                        keywordPageNum = keywordPageNum!=0?keywordPageNum:startPage;
                                        keywordContentAndPositionList =(List<ComponentResult.TextPosition>) containsKeyWordOfRegex2Lines.get("position");
                                        break;
                                    }
                                }
                                if (keywordFlag){
                                    ComponentResult keywordComponentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, subTile+"关键词检查-"+keyword, "正确", false, getRegulationId(), keywordPageNum, keywordContentAndPositionList, "01");
                                    componentResults.add(keywordComponentResult);
                                }else {
                                    isAllErr = true;
                                    ComponentResult keywordComponentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, subTile+"关键词检查-"+keyword, "缺失", true, getRegulationId(), startPage, keywordContentAndPositionList, "02");
                                    componentResults.add(keywordComponentResult);
                                }
                            }

                            //补充资本金

                            for (PageModel pageModel : pageModelList){
                                //Map<String, Object> containsKeyWordOfRegex2Lines = handingData.isContainsKeyWord2Lines(pageModel.getJsonTextData(), yjyaKeyword);

                                //Map<String, Object> containsKeyWord = handingData.isContainsKeyWord(pageModel.getJsonTextData(), yjyaKeyword);
                                Map<String, Object> containsKeyWordOfRegex2Lines = searchKeywordUtil.isContainsKeywords2LiensOfSynonyms(pageModel.getJsonTextData(), yjyaKeyword,pageModel);
                                yjyaFlag = (boolean)containsKeyWordOfRegex2Lines.get("flag");
                                if (yjyaFlag){
                                    yjyaPageNum = pageModel.getPageNumber();
                                    yjyaPageNum = yjyaPageNum!=0?yjyaPageNum:startPage;
                                    yjyaContentAndPositionList =(List<ComponentResult.TextPosition>) containsKeyWordOfRegex2Lines.get("position");
                                    break;
                                }
                            }
                            if (yjyaFlag){
                                ComponentResult keywordComponentResult = handingData.getComponentResult(catalogId,chapterId, ruleName,subTile+"关键词检查-"+yjyaKeyword, "正确", false, getRegulationId(), yjyaPageNum, yjyaContentAndPositionList, "01");
                                componentResults.add(keywordComponentResult);
                            }else {
                                isAllErr = true;
                                ComponentResult keywordComponentResult = handingData.getComponentResult(catalogId,chapterId, ruleName,subTile+"关键词检查-"+yjyaKeyword, "缺失", true, getRegulationId(), startPage, yjyaContentAndPositionList, "02");
                                componentResults.add(keywordComponentResult);
                            }
                        }
                    }
                }
            }

            if (bczjFlag){
                if (isAllErr){
                    for (ComponentResult result:componentResults){
                        result.setExist("02");
                    }
                }
            }else {
                componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTile+"关键词检查-"+bczjKeyword,"未检查到,请人工核对",true,getRegulationId(),startPage,bczjContentAndPositionList,"03");
                componentResults.add(componentResult);
                for (String key:keywords){
                    componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTile+"关键词检查-"+key,"未触发检查",true,getRegulationId(),startPage,bczjContentAndPositionList,"03");
                    componentResults.add(componentResult);
                }

                componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTile+"关键词检查-"+yjyaKeyword,"未触发检查",true,getRegulationId(),startPage,bczjContentAndPositionList,"03");
                componentResults.add(componentResult);
            }

        }catch (Exception e){
            componentResults.clear();
            componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTile+"关键词检查-"+bczjKeyword,"未识别,建议人工核对",true,getRegulationId(),startPage,bczjContentAndPositionList,"03");
            componentResults.add(componentResult);
            for (String key:keywords){
                componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTile+"关键词检查-"+key,"未触发检查",true,getRegulationId(),startPage,bczjContentAndPositionList,"03");
                componentResults.add(componentResult);
            }
            componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName,subTile+"关键词检查-"+yjyaKeyword,"未触发检查",true,getRegulationId(),startPage,bczjContentAndPositionList,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }
        return componentResults;
    }
}
